﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using MySql.Data;
using MySql.Data.MySqlClient;

namespace DAOLayer
{
    public class CDAOTransportType
    {
        private MySqlDataAdapter AdapterTransportType = null;
        public bool UpdatingData = false;

        /// <summary>
        /// Таблица с данными о типах транспорта
        /// </summary>
        public DataTable Table = new DataTable();
        
        /// <summary>
        /// Создание нового класса типов транспорта
        /// </summary>
        /// <param name="Connection">Подключение к базе данных</param>
        public CDAOTransportType(CDAOTransportions Transportions)
        {
            //Table.Locale.TextInfo.OEMCodePage = Encoding.UTF8;
            AdapterTransportType = new MySqlDataAdapter("SELECT transport_type_id, transport_type FROM s_transport_type;", Transportions.Connection);
            AdapterTransportType.UpdateCommand = new MySqlCommand("UPDATE s_transport_type SET transport_type=@VALUE WHERE transport_type_id=@ID", Transportions.Connection);
            AdapterTransportType.UpdateCommand.Parameters.Add(new MySqlParameter("VALUE", MySqlDbType.VarChar, 32, "transport_type"));
            AdapterTransportType.UpdateCommand.Parameters.Add(new MySqlParameter("ID", MySqlDbType.Int32, 10, "transport_type_id"));
            AdapterTransportType.DeleteCommand = new MySqlCommand("DELETE FROM s_transport_type WHERE transport_type_id=@ID", Transportions.Connection);
            AdapterTransportType.DeleteCommand.Parameters.Add(new MySqlParameter("ID", MySqlDbType.Int32, 10, "transport_type_id"));
            AdapterTransportType.InsertCommand = new MySqlCommand("INSERT INTO s_transport_type (transport_type) VALUES (@VALUE)", Transportions.Connection);
            AdapterTransportType.InsertCommand.Parameters.Add(new MySqlParameter("VALUE", MySqlDbType.VarChar, 32, "transport_type"));
            Table.Columns.Add("transport_type_id");
            Table.Columns.Add("transport_type");
            Table.Columns["transport_type_id"].AutoIncrementStep = 1;
            Table.Columns["transport_type_id"].AutoIncrement = true;
            AdapterTransportType.Fill(Table);
            Table.RowChanged += new DataRowChangeEventHandler(TableTransportTypeRowChanged);
            Table.RowDeleted += new DataRowChangeEventHandler(TableTransportTypeRowDelete);
            Table.TableNewRow += new DataTableNewRowEventHandler(TableTransportTypeRowInsert);
        }

        /// <summary>
        /// Обновить данные из базы данных
        /// </summary>
        public void Update()
        {
            UpdatingData = true;
            Table.Clear();
            AdapterTransportType.Fill(Table);
            UpdatingData = false;
        }

        public string this[int id]
        {
            get
            {
                if (Table == null || !Table.IsInitialized)
                    return "";
                else
                {
                    string id_string = id.ToString();
                    foreach (DataRow row in Table.Rows)
                        if ((string) row["transport_type_id"] == id_string)
                        {
                            return (string) row["transport_type"];
                        }
                    return "";
                }
            }
        }

        private void TableTransportTypeRowChanged(object sender, DataRowChangeEventArgs e)
        {
            if (UpdatingData) return;
            if (AdapterTransportType.UpdateCommand.Connection.State == ConnectionState.Open)
            {
                AdapterTransportType.UpdateCommand.Parameters["ID"].Value = e.Row["transport_type_id"];
                AdapterTransportType.UpdateCommand.Parameters["VALUE"].Value = e.Row["transport_type"].ToString();
                AdapterTransportType.UpdateCommand.ExecuteNonQuery();
            }
        }
        private void TableTransportTypeRowDelete(object sender, DataRowChangeEventArgs e)
        {
            if (UpdatingData) return;
            if (AdapterTransportType.DeleteCommand.Connection.State == ConnectionState.Open)
            {
                AdapterTransportType.DeleteCommand.Parameters["ID"].Value = e.Row["transport_type_id"];
                AdapterTransportType.DeleteCommand.ExecuteNonQuery();
            }
        }
        private void TableTransportTypeRowInsert(object sender, DataTableNewRowEventArgs e)
        {
            if (UpdatingData) return;
            if (AdapterTransportType.InsertCommand.Connection.State == ConnectionState.Open)
            {
                AdapterTransportType.InsertCommand.Parameters["VALUE"].Value = e.Row["transport_type"].ToString();
                AdapterTransportType.InsertCommand.ExecuteNonQuery();
            }
        }

    }
}
